package com.example.demo.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.example.demo.mapper.LoginRegistermapper;
import com.example.demo.pojo.medicaluser;
import com.example.demo.service.LoginRegisterservice;
import com.example.demo.utils.EncryptionUtils;
import com.example.demo.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginRegisterserviceimpl implements LoginRegisterservice {
    @Autowired
    LoginRegistermapper loginRegistermapper;
    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    EncryptionUtils passwordEncoder;


    public String login(String username, String password) {
        try{
           medicaluser user = loginRegistermapper.login(username,password);
            String password1 = loginRegistermapper.getPassword(username);
            if(passwordEncoder.encryptPassword(password).equals(password1)){
                StpUtil.login(username);
                return "ok";
            }
            if(user==null) {
                throw new RuntimeException();
            }
            StpUtil.login(username);
            return "ok";
        }catch (Exception e){
            throw new RuntimeException();

        }
    }

    public void register(medicaluser user) {
        try{
            user.setPassword(passwordEncoder.encryptPassword(user.getPassword()));

            loginRegistermapper.register(user);
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public String getPassword(String username) {
        return loginRegistermapper.getPassword(username);
    }
    public  String selectpost(String username){
      return loginRegistermapper.selectpost(username);

    }
}




